/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package amd.database;

import java.util.ArrayList;

/**
 *
 
 */
public class DbTerm {

    public static ArrayList<String> getAllTermContents(ConnectDB conn) throws QueryNoResultException {
        String sql = "SELECT term FROM terms ORDER BY term ASC";
        ArrayList<ArrayList<Object>> obj = conn.getAllDatas(sql);
        ArrayList<String> listTermContent = new ArrayList<String>();
        int i=0;
        while(i<obj.get(0).size()){
            listTermContent.add(obj.get(0).get(i).toString());
            i++;
        }
        return listTermContent;
    }

    public static ArrayList<String> getAllTerms(ConnectDB conn) throws QueryNoResultException {
        String sql = "SELECT term FROM terms ORDER BY term ASC";
        ArrayList<Object> obj = conn.getAllIdDatas(sql);
        ArrayList<String> listTermContent = new ArrayList<String>();
        int i=0;
        while(i<obj.size()){
            listTermContent.add(obj.get(i).toString());
            i++;
        }
        return listTermContent;
    }

    public static int getNTerm(ConnectDB conn) {
        int nTerm=conn.countRows("terms");
        return nTerm;
    }

//    public static void insert(ConnectDB conn){
//        String sql = "";
    public static void insert(ConnectDB conn, String termContent){
        String sql = "INSERT INTO terms (term) values (\'"+termContent+"\')";
        conn.createStatement(sql);
    }

    public static String getTermContent(ConnectDB conn, int index) throws QueryNoResultException {
        String sql = "SELECT term FROM terms ORDER BY term ASC LIMIT "+ index +",1";
        String termContent = conn.getSingleData(sql, 1).toString();
        return termContent;
    }
    public static boolean isExist(ConnectDB conn, String term) throws QueryNoResultException {
        boolean exist = false;
        String sql = "SELECT term FROM terms WHERE term = \'"+term+"\'";
        String termContent = conn.getSingleData(sql, 1).toString();
        if(termContent!=null){
            exist = true;
        }
        return exist;
    }

}
